<!DOCTYPE html>
<html>
<head>
    <title>发药</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script type="text/javascript" src="../../Js/ckeditor/ckeditor.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form action="dispensing.html" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">病人号</td>
            <td name="behId">1101</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">姓名</td>
            <td name="behName">黄飞鸿</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">药品名称</td>
            <td>
                <select name="drId">

                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">发药数量</td>
            <td>
                <input type="text" name="assignDrugNum" value=""/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button class="btn btn-primary" type="button" id="save">保存</button> &nbsp;&nbsp;
                    <button type="button" class="btn btn-success" id="returnList">返回列表</button>
                </center>
            </td>
        </tr>
    </table>
</form>
</body>
<script type="text/javascript">

    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }

    $(function () {

        let url = document.location.toString();
        let pageNum=url.GetValue("pageNum");
        let behId=url.GetValue("behId");

        loadDrug();
        loadPeople(behId);

        //返回
        $('#returnList').click(function () {
            window.location.href = "dispensing.html?pageNum="+pageNum;
        });

        //保存
        $('#save').click(function () {
            submit(pageNum);
        });

    });

    //提交
    function submit(pageNum) {

        let behId = $("td[name='behId']").html();
        let behName = $("td[name='behName']").html();
        let assignDrugNum = $("input[name='assignDrugNum']").val();
        let drId = $("select[name='drId']").val();


        if(confirm("对 "+behName+" 病人进行开药？")){
            $.ajax({
                type : "post",
                url : "/drugpeople/assignDrug",
                data : {
                    behId:behId,
                    behName:behName,
                    assignDrugNum:assignDrugNum,
                    drId:drId
                },
                dataType : "json",
                success: function (vo) {
                    if(vo.code === 200){
                        alert("开药成功！");
                        window.location.href = "dispensing.html?pageNum="+pageNum;
                    }else{
                        alert("开药异常！"+vo.msg);
                    }
                }
            });
        }
    }

    //加载药物
    function loadDrug() {
        $.ajax({
            type : "get",
            url : "/drug/all",
            data : "",
            dataType : "json",
            success: function (vo) {
                let list = vo.list;
                let str = "<option value='0'>请选择药物</option>";
                for (let i = 0; i < list.length ; i++) {
                    let obj = list[i];
                    let drId = obj.drId;
                    let drName = obj.drFactory+" "+obj.drName;
                    str += "<option value='"+drId+"'>"+drId+" - "+drName+"</option>";
                }
                $("select[name='drId']").html(str);
            }
        });
    }

    // 加载角色信息
    function loadPeople(behId){
        $.ajax({
            type : "get",
            url : "/behospital/"+behId,
            data : "",
            dataType : "json",
            success: function (vo) {
                let obj = vo.obj;
                $("td[name='behId']").html(obj.behId);
                $("td[name='behName']").html(obj.behName);
            }
        });
    }

</script>
</html>